<?php

class IndexController extends Zend_Controller_Action
{

    /**
     * FlashMessenger
     *
     * @var Zend_Controller_Action_Helper_FlashMessenger
     */
    protected $_flashMessenger = null;

    public function init()
    {
        $this->_helper->layout()->setLayout('new-kids-layout');
        //        $this->_helper->layout()->disableLayout();

        $this->_flashMessenger = $this->_helper->getHelper('FlashMessenger');

        // [0] Locale
        $localeSession = new Zend_Session_Namespace('locale');
        $lang = $localeSession->locale;
        $this->view->locale = $lang;
        $localesManager = new \Managers\Locales();
        $locale = $localesManager->getByLocaleName($lang);
        // [0]

        // [1] Main menu.
        $pagesManager = new \Managers\Pages();
        $menu = $pagesManager->getMenu($locale['id']);        
        
        $this->view->menu = $menu;
        // [1]
    }

    public function indexAction()
    {
        $this->_forward('page');
    }


    public function pageAction()
    {
        $id = intval($this->_getParam('id'));
        if (!$id) {
            $localeSession = new Zend_Session_Namespace('locale');
            $lang = $localeSession->locale;
            if ($lang == 'ru') {
                $id = 3;
            } else {
                $id = 2;
            }
        }

        $pagesManager = new \Managers\Pages();
        $page = $pagesManager->getById($id);
        $this->view->page = $page;
        
        $pageSession = new Zend_Session_Namespace('pagesession');
        $pageSession->current = $id;

        $this->view->current= $id;

        // Photogallary.
        if ($page['type']['name'] == 'Photo') {
            $this->renderScript('/index/photo.phtml');
        }
          // General.
        if ($page['title'] == 'Общее') {
            $this->renderScript('/index/general_ru.phtml');
        }
        if ($page['title'] == 'General') {
            $this->renderScript('/index/general_en.phtml');
        }


    }

    public function localeAction()
    {
        $locale = $this->_getParam('locale', 'en');

        if ($locale == 'en' || $locale == 'ru') {
            $localeSession = new Zend_Session_Namespace('locale');
            $localeSession->locale = $locale;
            $pageSession = new Zend_Session_Namespace('pagesession');
            $old_page = $pageSession->current;
            $pagesManager = new \Managers\Pages();
            $page = $pagesManager->getById($old_page);
            $type = $page['type'];

            $localeManager = new \Managers\Locales();
            $locale = $localeManager->getByLocaleName($locale);

            $new_page = $pagesManager->getByTypeLocale($type['id'], $locale['id']);
            $this->_redirect('/page/' . $new_page['id']);
        }
    }


}
